package com.libl.controller;

import com.libl.pojo.DtsAdmin;
import com.libl.pojo.DtsComment;
import com.libl.service.DtsBrandService;
import com.libl.service.DtsCommentService;
import com.libl.util.ResponseUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/comment")
public class DtsCommentController {
    @Autowired
    DtsBrandService brandService;
    @Autowired
    DtsCommentService commentService;

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false, defaultValue = "1") Integer page,
                       @RequestParam(value = "limit",required = false, defaultValue = "20") Integer limit,
                       Integer userId , Integer valueId, String sort, String order){

        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;
        //如果该管理员是品牌制造商，只能查询对应品牌的评论信息
        if(subject.hasRole("品牌制造商")){
            brandIds = brandService.findBrandIdsByAdminId(admin.getId());
        }

        Map<String,Object> data = commentService.queryPage(page,limit, userId, valueId, sort, order, brandIds);
        return ResponseUtil.ok(data);
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsComment comment){
        commentService.delete(comment.getId());
        return ResponseUtil.ok();
    }

}
